//
//  VRTabBarViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/11.
//

import UIKit

class VRTabBarViewController: UITabBarController {

    let pages: [UIViewController] = [VRHomeViewController(), VRDiscoverViewController(), VRMessageViewController(), VRProfileViewController()]
    
    let imageName: [String] = ["home", "discover", "msg", "me"]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.tabBar.backgroundImage = UIImage()
        self.tabBar.backgroundColor = .black
        
        //这个按钮就是中间添加的按钮
        let pushBtn = UIButton(type: .custom)
        pushBtn.frame = CGRect(x: .screenWidth/5 * 4 , y: -20, width: 65, height: 65)
        pushBtn.setImage(UIImage(named: "TabBar_push"), for: .normal)
        pushBtn.addTarget(self, action: #selector(pushBtnAction), for: .touchUpInside)
//        middleBtn.backgroundColor = .red

        let home = configPages(page: pages[0], name: imageName[0])
        let discover = configPages(page: pages[1], name: imageName[1])
        let message = configPages(page: pages[2], name: imageName[2])
        let me = configPages(page: pages[3], name: imageName[3])

        let middle = UIViewController()//占位用
        middle.tabBarItem.isEnabled = false

//        viewControllers = [home,flower,middle,message,me]
        addChild(home)
        addChild(discover)
        addChild(message)
        addChild(me)
        addChild(middle)
        tabBar.shadowImage = UIImage()
        tabBar.backgroundColor = .black
        tabBar.addSubview(pushBtn)
        tabBar.bringSubviewToFront(pushBtn)
        selectedIndex = 0
        delegate = self
    }
    
    
    private func configPages(page: UIViewController, name: String) -> VRNavViewController {
        page.tabBarItem.image = "TabBar_\(name)".toImage?.withRenderingMode(.alwaysOriginal)
        page.tabBarItem.selectedImage = "TabBar_\(name)_sel".toImage?.withRenderingMode(.alwaysOriginal)
        let nav = VRNavViewController(rootViewController: page)
        return nav
    }
    
    @objc private func pushBtnAction(){
//        self.navigationController?.pushViewController(VRHomeSearchViewController(), animated: true)
        self.present(VRPushViewController(), animated: true)
    }
}

extension VRTabBarViewController: UITabBarControllerDelegate {
    //是否可以点击
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        return true
    }
    //点击其他四个item 代理方法
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)   {
      
    }
}
